/*
 * create on 2008-8-27
 * Copy right (2008)
 * Author Tan Nailiang
 * HNA Systems All rights reserved
 */
package com.hnas.core.jms;

import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author 郑兴(zhengxing)
 *
 */
public class JmsConsumer implements MessageListener{
	private Logger log = LoggerFactory.getLogger(JmsConsumer.class);
	
	private Map<String,JmsHandler> handlers;

	/**
	 * @param handlers the handlers to set
	 */
	public void setHandlers(Map<String, JmsHandler> handlers) {
		this.handlers = handlers;
	}
	
	/* (non-Javadoc)
	 * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
	 */
	public void onMessage(Message message) {
		log.debug(message.getClass().getSimpleName());
		ObjectMessage objMessage = (ObjectMessage) message;
		try {
			JobArtifact artifact = (JobArtifact) objMessage.getObject();
			log.info("begin handle:"+artifact.getClass().getSimpleName());
			if (handlers.containsKey(artifact.getClass().getSimpleName())) {
				JmsHandler handler = handlers.get(artifact.getClass().getSimpleName());
				log.debug(handler.toString());
				handler.handle(artifact);
			}
			log.info("after handle:"+artifact.getClass().getSimpleName());

		} catch (JMSException e) {
			log.error("Jms consume error", e);
		}
	catch (JmsException e2) {
		log.error("Jms consume error2 ", e2);
	}
	}
}
